function LinkMonitor(){
	var links = new Array();            // An array of Links to monitor
	var checkedHostnames = new Array(); // An array of hostnames that we have already checked (False = No HTTPS, True = HTTPS)
	
	
    chrome.extension.sendMessage({action: "PageVisited", hostname: window.location.hostname});
    
	
	
	/**
	 * Calculates the ratio of secure to total links.
	 * @author Daniel Fairweather
	 * @returns {JSON} The number of safe links and the total number of links.
	 */
    this.getEffectiveness = function(){
        var total = links.length;
        var safe = 0;
        
        for(var i = 0; i < links.length; i++){
            if(links[i].getProtocol() == "https"){
                safe++;
            }
        }
        
        return({"safe":safe, "total":total});
    }
	
	
	
	/**
	 * Adds the link to the links array and attempts to secure and monitor the link.
	 * @author Daniel Fairweather
	 * @param link The link to be secured and monitored.
	 */
	this.addLink = function(link){
		// We need to send some info to the background page (For statistics)
		// One addLink function corresponds to one link identified
		chrome.extension.sendMessage({action: "LinkDetected", hostname: link.getHostname()});
		links.push(link);
		secure(link);
		setupActionMonitor(link);
	}
	
	
	
	/**
	 * Creates an event listener for the action change event. Preforms a second check when action is changed.
	 * @author Daniel Fairweather
	 * @param link The link to be monitored.
	 */
	var setupActionMonitor = function(link){
		var monitor = new WebKitMutationObserver(function(){
            link.update();
          
            if(link.status == "Secure" && link.getProtocol() == "http"){
				link.status = "Suspicious";
				secure(link);
			}
			
			else if(link.status == "Suspicious" && link.getProtocol() == "http"){
				link.status = "Compromised";
				this.disconnect();
				if(extension.compromised != true){
					alert("WARNING: CAT+S has detected a script which is preventing HTTPS links on this website. It is advised that you navigate away from this page.");
					extension.compromised = true;
				}
			}
			
			if(link.getProtocol() != "https" && link.status != "Compromised"){
				secure(link);
            }
		});
	
		monitor.observe(link.getForm(), {attributes: true});
	}
	
	
	
	/**
	 * Attempts to make a secure connection with the website. If a secure connection is possible,
	 * the link is converted to HTTPS. Note that there is no need to secure HTTPS sites.
	 * @author Henry Mozer
	 * @author Daniel Fairweather
	 * @param link The link to be secured.
	 */
	var secure = function(link){
		var request = new XMLHttpRequest();

        // If we already know the link can be HTTPS (from a previous check), just set it to HTTPS.
        if(link.getProtocol() != "https" && checkedHostnames[link.getHostname()] == true){
            link.setHTTPS();
        }
        
		// We only need to check links that are not HTTPS
		else if(link.getProtocol() != "https" && checkedHostnames[link.getHostname()] != false){
			// Try to make an HTTPS connection to the link
			request.open("HEAD", "https://"+link.getHostname(), true);
		
			// If the connection worked, set the link to HTTPS
			request.onload = function(){
                checkedHostnames[link.getHostname()] = true;
				link.setHTTPS();
				updateStatistic("LinkSecured", link.getHostname());
			};
            
            // If the connection didn't work, remember that it has no HTTPS alternative
            request.onerror = function(){
				if(checkedHostnames[link.getHostname()] == undefined){
					checkedHostnames[link.getHostname()] = false;
					updateStatistic("LinkInsecure", link.getHostname());
					link.status = "Insecure";
				}
            };

			request.send(null);
			console.log("Sent request to: " + link.toString());
		}
		
		// The Link is already secure
		else{
			updateStatistic("LinkAlreadySecure", link.getHostname());
		}
	}
	
	
	
	/**
	 * TODO
	 */
	var updateStatistic = function(message, hostname){
		chrome.extension.sendMessage({"message": message, "hostname": hostname});
	}
}